<?php
/**
 * Language switcher helper
 */
class Helper_LangSwitcher
{
    public $view;
    
    public function setView(Zend_View_Interface $view)
    {
        $this->view = $view;
    }
    
    public function langSwitcher()
    {
        $languages=Zend_Registry::get('config')->site->locales->toArray();

        $language=Zend_Registry::get('language');
        
        if (count($languages) < 2) return;
        
        $urlParams=explode('/',trim($this->view->url(),'/'));

        if (Application_Model_Locale::isValidLocale($urlParams[0])) array_shift($urlParams);

        $url=implode('/',$urlParams);

        $switcher='<ul id="lang-switcher">';

        foreach ($languages as $code=>$name)
        {
            $lang_prefix=$code != $language->getDefaultLocale() ? $code.'/' : '';
        
            if (!empty($lang_prefix) && empty($url)) $url='index';
        
            $switcher.='<li><a href="'.$this->view->baseUrl($lang_prefix.$url).'">'.htmlspecialchars($name).'</a></li>';
        }
        
        $switcher.='</ul>';
        
        return $switcher;
    }
}
